package com.rjpacket.MyUEnjoy.fragments;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.rjpacket.MyUEnjoy.Product;
import com.rjpacket.MyUEnjoy.R;
import com.rjpacket.MyUEnjoy.StoreDetailActivity;
import com.rjpacket.MyUEnjoy.StoreProduct;
import com.rjpacket.MyUEnjoy.adapters.StoreListBaseAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by aaa on 15-1-23.
 */
public class ImportStoreFragment extends Fragment implements AdapterView.OnItemClickListener {
    private static final String path = "Http://open.doushangshang.com/";
//    private SimpleAdapter adapter;
    private List<Map<String, Object>> data;
    private StoreListBaseAdapter adapter;

    public ImportStoreFragment(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.import_store_fragment, container, false);

        ListView listView = (ListView) view.findViewById(R.id.listView_store);

        data = new LinkedList<Map<String, Object>>();

//        adapter = new SimpleAdapter(
//                getActivity(),
//                data,
//                R.layout.item_product,
//                new String[]{"id","name"},
//                new int[]{R.id.product_id,R.id.product_name}
//        );
        adapter = new StoreListBaseAdapter(getActivity(),data);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        StoreTask task = new StoreTask();
        task.execute(path+"category.php?id=1&size=20");

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(view != null){
            Intent intent = new Intent(getActivity(), StoreDetailActivity.class);
            Map<String, Object> map = data.get(position);
            String url = (String)map.get("url");
            intent.putExtra("url",url);
            startActivityForResult(intent, 1024);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1024){

        }
    }

    private class StoreTask extends AsyncTask<String,Integer,List<Product>>{

        @Override
        protected List<Product> doInBackground(String... params) {
            List<Product> list = new LinkedList<Product>();
            String proUrl = null;
            if(params != null && params.length>0){
                proUrl = params[0];
                HttpGet request = new HttpGet(proUrl);
                HttpClient client = new DefaultHttpClient();

                try {
                    HttpResponse response = client.execute(request);
                    int state = response.getStatusLine().getStatusCode();
                    if(state == 200){
                        HttpEntity entity = response.getEntity();
                        byte[] bytes = EntityUtils.toByteArray(entity);

                        //Json
                        JSONObject result = new JSONObject(new String(bytes));
                        JSONObject msgObj = result.getJSONObject("msg");
                        JSONObject dataObj = msgObj.getJSONObject("data");
                        for(int i=122;i<133;i++){
                            JSONObject itemPro = dataObj.getJSONObject(i+"");
                            if(itemPro != null) {
                                Product product = new Product();
                                product.setGoods_id(itemPro.getString("goods_id"));
                                product.setName(itemPro.getString("name"));
                                product.setGoods_style_name(itemPro.getString("goods_style_name"));
                                product.setGoods_name(itemPro.getString("goods_name"));
                                product.setMarket_price(itemPro.getString("market_price"));
                                product.setShop_price(itemPro.getString("shop_price"));
                                product.setAdd_time(itemPro.getString("add_time"));
                                product.setGoods_thumb(itemPro.getString("goods_thumb"));
                                product.setGoods_img(itemPro.getString("goods_img"));
                                product.setUrl(itemPro.getString("url"));
                                list.add(product);
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return list;
        }

        @Override
        protected void onPostExecute(List<Product> storeProducts) {
            if(storeProducts != null){
                Map<String,Object> map = null;
                for(Product pro:storeProducts){
                    map = new HashMap<String, Object>();
                    map.put("id",pro.getGoods_id());
                    map.put("name",pro.getName());
                    map.put("goods_thumb",pro.getGoods_thumb());
                    String url = pro.getUrl();
                    map.put("url", url);
                    data.add(map);
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

    //获取内容
}